package com.happts.Dao;

import com.happts.Entity.RECORD;
import com.happts.Utils.JDBCManager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class RECORDDao {

    public boolean insert(RECORD record){
        String sql = "insert into RECORD (NAME,ADDRESS,CITE, NUMBER, WEIGHT, VEGRATE, LEFTVEG, REFINEDRATE, LEFTREFINED) values(?,?,?,?,?,?,?,?,?)";
        Object[] params = {
                record.getName(),record.getAddress(),record.getCite(),record.getNumber(),record.getWeight(),record.getVegRate(),record.getLeftVeg(),record.getRefinedRate(),record.getLeftRefined()
        };
        return JDBCManager.executeSQL(sql,params);
    }

    public boolean delete(String CITE,String NUMBER){
        String sql = "DELETE FROM RECORD WHERE NUMBER=? AND CITE=?";
        Object[] params = {NUMBER,CITE};
        return JDBCManager.executeSQL(sql,params);
    }

    public boolean update(RECORD record){
        String sql = "update RECORD set WEIGHT=?, LEFTVEG=?, LEFTREFINED=? where NUMBER=? and CITE=?";
        Object[] params = {
          record.getWeight(), record.getLeftVeg(), record.getLeftRefined(), record.getNumber(), record.getCite()
        };
        return JDBCManager.executeSQL(sql,params);
    }

    public List<RECORD> findALL(){
        String sql = "SELECT * FROM RECORD";
        ResultSet resultSet = JDBCManager.executeQuery(sql);
        return resultSetToList(resultSet);
    }
    //单属性模糊查询
    public List<RECORD> find(String columnLable,String value){
        String sql = "SELECT * FROM RECORD WHERE "+ columnLable + " LIKE "+ "\'%"+ value +"%\'";
        ResultSet resultset = JDBCManager.executeQuery(sql);
        return resultSetToList(resultset);
    }

    //精准查询,主键
    public List<RECORD> findOne(String CITE,String NUMBER){
        String sql = "SELECT * FROM RECORD WHERE CITE=? AND NUMBER=?";
        Object[] params = {
                CITE,NUMBER
        };
        ResultSet resultSet = JDBCManager.executeQuery(sql,params);
        return resultSetToList(resultSet);
    }
    private List<RECORD> resultSetToList(ResultSet resultSet){
        List<RECORD> recordList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                RECORD record = new RECORD(resultSet.getString("NAME"),
                        resultSet.getString("ADDRESS"),
                        resultSet.getString("CITE"),
                        resultSet.getString("NUMBER"),
                        resultSet.getInt("WEIGHT"),
                        resultSet.getDouble("VEGRATE"),
                        resultSet.getDouble("LEFTVEG"),
                        resultSet.getDouble("REFINEDRATE"),
                        resultSet.getDouble("LEFTREFINED"));
                recordList.add(record);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return recordList;
    }
}
